package com.yzk.aigy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yzk.aigy.mapper.SysUserMapper;
import com.yzk.aigy.model.system.SysUser;
import com.yzk.aigy.model.vo.RouterVo;
import com.yzk.aigy.model.vo.SysUserQueryVo;
import com.yzk.aigy.service.SysMenuService;
import com.yzk.aigy.service.SysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @description 针对表【sys_user(用户表)】的数据库操作Service实现
*/
@Service
@RequiredArgsConstructor
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser>
    implements SysUserService{

    private final SysUserMapper sysUserMapper;
    private final SysMenuService sysMenuService;

    @Override
    public IPage<SysUser> page(SysUserQueryVo sysUserQueryVo) {
        IPage<SysUser> page = new Page<>(sysUserQueryVo.getPageNum(), sysUserQueryVo.getPageSize());
        sysUserMapper.selectPage(page, sysUserQueryVo);
        return page;
    }

    @Override
    public SysUser getUserInfoUserName(String username) {
        return sysUserMapper.selectOne(Wrappers.<SysUser>lambdaQuery().eq(SysUser::getUsername, username));
    }

    @Override
    public Map<String, Object> getUserInfo(String username) {
        Map<String, Object> map = new HashMap<>();
        SysUser sysUser = this.getUserInfoUserName(username);

        //根据用户id获取菜单权限值
        List<RouterVo> routerVoList = sysMenuService.findUserMenuList(sysUser.getId());
        //根据用户id获取用户按钮权限
        List<String> permsList = sysMenuService.findUserPermsList(sysUser.getId());

        //当前权限控制使用不到，我们暂时忽略
        map.put("name", sysUser.getName());
        map.put("avatar", "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        map.put("roles",  "[admin]");
        map.put("buttons", permsList);
        map.put("routers", routerVoList);
        return map;
    }

}




